<template>
  <div class="pr-dictionaries">
    <div class="pr-dictionaries-head">
      <el-input v-model="searchData.name" placeholder="名称" clearable @change="getDicList" />
      <el-button @click="getDicList">查询</el-button>
    </div>
    <div>
      <el-table :data="tableData" border fit stripe highlight-current-row>
        <el-table-column v-for="item in tableHead" :key="item.id" :label="item.label" :prop="item.title" align="center">
          <template slot-scope="scope">
            {{ scope.row[item.title] }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="400" align="center">
          <template slot-scope="scope">
            <el-button size="mini" @click="amend(scope.row)">编辑</el-button>
            <el-button size="mini" @click="delDict(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination :current-page="currentPage" :page-sizes="[10, 30, 50, 100]" :page-size="searchData.size" layout="total, sizes"
        :total="totalCount" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
    </div>

    <!-- 编辑子条目弹框 -->
    <el-dialog title="编辑子条目" :modal='false' :close-on-click-modal="false" :visible.sync="amendVisible" width="40%">
      <edit-dialog :row="data" :dialog='amendVisible' @chang="amendDialogChang" />
    </el-dialog>

  </div>
</template>

<script>
  import EditDialog from './edit.vue'
  export default {
    props: ["row", "dialog"],
    components: {
      EditDialog,
    },
    data() {
      return {
        data: {},
        list: null,
        amendVisible: false, // 修改弹框显示隐藏
        currentPage: 1,
        tableHead: [{
            id: 1,
            label: '名称',
            title: 'name'
          },
          // {
          //   id: 2,
          //   label: '字典编码',
          //   title: 'code'
          // },
          // {
          //   id: 3,
          //   label: '详情',
          //   title: 'detail'
          // },
          {
            id: 4,
            label: '备注',
            title: 'description'
          }
        ],
        tableData: [],
        searchData: {
          name: '',
          current: 1,
          size: 10,
          pCode: ''
        },
        totalCount: 10,
      }
    },
    watch: {
      dialog: {
        handler() {
          if (this.dialog) {
            this.searchData.pCode = this.row.code;
            this.getDicList()
          }
        },
        immediate: true, // immediate选项可以开启首次赋值监听
        deep: true
      }
    },
    methods: {
      // 查询
      getDicList() {
        this.$server.getDicList(this.searchData).then(res => {
          this.tableData = res.data;
          this.totalPage = res.data.totalPage;
          this.totalCount = res.data.totalCount;
        });
      },
      amend(e) {
        this.data = e
        this.amendVisible = true
      },
      // 确认修改子条目
      amendDialogChang(e) {
        this.amendVisible = false
        if (e.flag) {
          this.getDicList()
          console.log(e.flag)
        } else {
          console.log(e.flag)
        }
      },
      // 删除
      delDict(data) {
        let that = this;
        this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          })
          .then(() => {
            that.$server.deleteDict({
              id: data.id
            }).then(res => {
              that.$message({
                type: "success",
                message: "删除成功!"
              });
              that.getDicList();
            });
          })
          .catch(() => {
            that.$message({
              type: "info",
              message: "已取消删除"
            });
          });
      },
      handleSizeChange(val) {
        console.log(`每页 ${val} 条`)
      },
      handleCurrentChange(val) {
        console.log(`当前页: ${val}`)
      }
    }
  }
</script>
<style lang="scss" scope>
  .pr-dictionaries {
    &-head {
      display: flex;
      padding: 20px;

      >div {
        margin-right: 20px;
      }

      .el-cascader {
        width: 200px;
      }

      .el-input {
        width: 200px;
      }
    }
  }

  .pr-dialog-assignRoles {
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .el-pagination {
    margin-top: 20px;
  }
</style>
